<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/views/common/taglibs.jsp" %>

<!DOCTYPE>
<html>
<head>
    <!-- 引入通用js、css文件(xiaogq) -->
    <meta name="decorator" content="default"/>
    <link rel="stylesheet" type="text/css" href="${ctx}/css/common/operation/sys_operation.css"/>
    <style type="text/css">
        .custom0{
            width: 80%;
            margin: 10px 10px;
        }
        .custom0>h4{
            color: #0D9BF2;
        }
        .custom0>hr{
            border: 2px solid #0D9BF2;
        }
        .customProperty1{
            padding:0 1%;
            line-height: 34px;
        }
        .customProperty1>.col-lg-3 span{
            color: red;
            position: absolute;
            font-size: 25px;
            top: 6px;
            left: 1px;
        }
    </style>
</head>
<body>
<div class="button_list">
    <a href="javascript:;" tableCode="${entityId}" onclick="saveFiled(this);">
            <span class="span_button">
                <span><img src="${ctx}/image/common/operation/oper_SAVE.png"></span>
                <span name="buTitle">保存</span>
            </span>
    </a>
    <a href="javascript:;" onclick="javascript:parent.closeTab();">
            <span class="span_button">
                <span><img src="${ctx}/image/common/operation/oper_CANCEL.png"></span>
                <span name="buTitle">取消</span>
            </span>
    </a>
</div>

<div class="custom0">
    <h4>自定义自定义属性和保存信息的类型</h4>
    <hr>
    <form id="form_field" action="" method="post">
        <input type="hidden" value="${entityId}" name="entityRecord.tableCode" />
        <input type="hidden" value="${entityLocalization}" name="entityRecord.inchinese" />
        <input type="hidden" value="${fieldTypeId}" name="fieldTypeId" />
        <div class="customProperty1 row">
            <div class="col-lg-1 col-md-1">
                <p>属性名(英文):</p>
            </div>
            <div class="col-lg-3 col-md-3">
                <span>*</span><input type="text" id="fieldName" id="" name="name" class="form-control"/>
            </div>
            <div class="col-lg-1 col-md-1">
                <p>属性名:</p>
            </div>
            <div class="col-lg-3 col-md-3">
                <span>*</span><input type="text" id="inchinese" name="inchinese" class="form-control"/>
            </div>
            <div class="col-lg-1 col-md-1">
                <p>描述:</p>
            </div>
            <div class="col-lg-3 col-md-3">
                <input type="text" id="comment" name="comment" class="form-control">
            </div>
        </div>
        <c:if test="${fieldTypeId != 5 && fieldTypeId != 6 && fieldTypeId != 7 && fieldTypeId != 8 && fieldTypeId != 9 && fieldTypeId != 10 && fieldTypeId != 11
                && fieldTypeId != 12 && fieldTypeId != 13 && fieldTypeId != 14 && fieldTypeId != 15 && fieldTypeId != 16 && fieldTypeId != 17 && fieldTypeId != 18}">
            <div class="customProperty1 row">
                <div class="col-lg-1 col-md-1">
                    <p>长度:</p>
                </div>
                <div class="col-lg-3 col-md-3">
                    <c:choose>
                        <c:when test="${fieldTypeId == 1 || fieldTypeId == 2 || fieldTypeId == 3}">
                            <input type="text" value="13" id="length"  name="length" class="form-control num-input">
                        </c:when>
                        <c:when test="${fieldTypeId == 19 || fieldTypeId == 20}">
                            <input type="text" value="20" id="length"  name="length" class="form-control num-input">
                        </c:when>
                        <c:otherwise>
                            <input type="text" value="255" id="length"  name="length" class="form-control num-input">
                        </c:otherwise>
                    </c:choose>
                </div>
            </div>
        </c:if>
        <c:if test="${fieldTypeId == 1 || fieldTypeId == 2 || fieldTypeId == 3}">
            <div class="customProperty1 row">
                <div class="col-lg-1 col-md-1">
                    <p>小数位数:</p>
                </div>
                <div class="col-lg-3 col-md-3">
                    <input type="text" id="precision" name="precision" value="2" class="form-control num-input">
                </div>
            </div>
        </c:if>
        <c:if test="${fieldTypeId != 5 && fieldTypeId != 6 && fieldTypeId != 8 && fieldTypeId != 9 && fieldTypeId != 10 && fieldTypeId != 11
                && fieldTypeId != 12 && fieldTypeId != 13 && fieldTypeId != 14 && fieldTypeId != 15 && fieldTypeId != 17 && fieldTypeId != 18 && fieldTypeId != 19 && fieldTypeId != 20}">
            <div class="customProperty1 row">
                <div class="col-lg-1 col-md-1">
                    <p>默认值:</p>
                </div>
                <div class="col-lg-3 col-md-3">
                    <c:choose>
                        <c:when test="${fieldTypeId == 1 || fieldTypeId == 2 || fieldTypeId == 3}">
                            <input type="text" id="defaultValue" name="defaultValue" class="form-control num-input">
                        </c:when>
                        <c:when test="${fieldTypeId == 4}">
                            <input type="text" id="defaultValue" name="defaultValue" class="form-control">
                        </c:when>
                        <c:otherwise>
                            <input type="checkbox" id="defaultValue" name="defaultValue">选中
                        </c:otherwise>
                    </c:choose>
                </div>
            </div>
        </c:if>
        <c:if test="${fieldTypeId == 7 || fieldTypeId == 8}">
            <div class="customProperty1 row">
                <div class="col-lg-1 col-md-1">
                    <p>编辑行数:</p>
                </div>
                <div class="col-lg-3 col-md-3">
                    <input type="text" class="form-control num-input" id="editRowCount" name="editRowCount" value="1">
                </div>
            </div>
        </c:if>
        <c:if test="${fieldTypeId == 9}">
            <div class="customProperty1 row">
                <div class="col-lg-1 col-md-1">
                    <p>引用对象名称:</p>
                </div>
                <div class="col-lg-3 col-md-3">
                    <select id="canQuote" name="quoteEntity.tableCode" class="form-control">
                        <c:forEach items="${canQuoteEntity}" var="item" >
                            <option value="${item.tableCode}" >${item.inchinese}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
        </c:if>
        <c:if test="${fieldTypeId == 11 || fieldTypeId == 12 || fieldTypeId == 17}">
            <div class="customProperty1 row">
                <div class="col-lg-1 col-md-1">
                    <p>枚举值共享属性：</p>
                </div>
                <div class="col-lg-3 col-md-3">
                    <select id="shareFieldId" name="shareFieldId" class="form-control">
                        <option value="">&nbsp;&nbsp;</option>
                        <c:forEach items="${frs}" var="item">
                            <option value="${item.id}">${item.inchinese} </option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <c:if test="${fieldTypeId != 17}">
                <div class="customProperty1 row">
                    <div class="col-lg-1 col-md-1">
                        <p>第一个为默认值:</p>
                    </div>
                    <div class="col-lg-3 col-md-3">
                        <input type="checkbox" value="1" id="firstAsDefault" name="firstAsDefault">
                    </div>
                </div>
            </c:if>
        </c:if>
        <c:if test="${fieldTypeId == 13 || fieldTypeId == 14}">
            <div class="customProperty1 row">
                <div class="col-lg-1 col-md-1">
                    <p>默认值:</p>
                </div>
                <div class="col-lg-3 col-md-3">
                    <select  class="form-control" id="dateType" name="dateType">
                        <option value="0"></option>
                        <option value="1">当前日期</option>
                        <option value="2">本月第一天</option>
                        <option value="3">本月最后一天</option>
                    </select>
                </div>
            </div>
        </c:if>
        <c:if test="${fieldTypeId == 15}">
            <div class="customProperty1 row">
                <div class="col-lg-1 col-md-1">
                    <p>显示格式:</p>
                </div>
                <div class="col-lg-3 col-md-3">
                    <label>如:{YYYY}{MM}{DD}-{0000}</label>
                </div>
            </div>
        </c:if>
        <!-- - - - ↓ 下面是复选框 - - - -->
        <c:if test="${fieldTypeId != 12 && fieldTypeId != 17 && fieldTypeId != 18}">
            <div class="customProperty1 row">
                <div class="col-lg-1 col-md-1">
                    <p>必填:</p>
                </div>
                <div class="col-lg-3 col-md-3">
                    <input type="checkbox" id="nulldisable" name="nulldisable">
                </div>
            </div>
        </c:if>
        <c:if test="${fieldTypeId != 7 && fieldTypeId != 8 && fieldTypeId != 15 && fieldTypeId != 18 && fieldTypeId != 19 && fieldTypeId != 20}">
            <div class="customProperty1 row">
                <div class="col-lg-1 col-md-1">
                    <p>批量更新:</p>
                </div>
                <div class="col-lg-3 col-md-3">
                    <input type="checkbox" id="batchUpdate" name="batchUpdate">
                </div>
            </div>
        </c:if>
        <c:if test="${fieldTypeId == 4 || fieldTypeId == 5 || fieldTypeId == 6 || fieldTypeId == 10 || fieldTypeId == 19 || fieldTypeId == 20}">
            <div class="customProperty1 row">
                <div class="col-lg-1 col-md-1">
                    <p>取值唯一:</p>
                </div>
                <div class="col-lg-3 col-md-3">
                    <input type="checkbox" <c:if test="${fieldTypeId == 19 || fieldTypeId == 20}">checked</c:if> id="valueUnique" name="valueUnique"/>
                </div>
            </div>
            <c:if test="${fieldTypeId == 4}">
                <div class="customProperty1 row">
                    <div class="col-lg-1 col-md-1">
                        <p>大小写敏感:</p>
                    </div>
                    <div class="col-lg-3 col-md-3">
                        <input type="checkbox" id="caseSensitive" name="caseSensitive">
                    </div>
                </div>
            </c:if>
        </c:if>
        <div class="customProperty1 row">
            <div class="col-lg-1 col-md-1">
                <p>属性控制:</p>
            </div>
            <div class="col-lg-3 col-md-3">
                <input type="checkbox" id="controled" name="controled">
            </div>
        </div>
        <div class="customProperty1 row">
            <div class="col-lg-1 col-md-1">
                <p>新建只读:</p>
            </div>
            <div class="col-lg-3 col-md-3">
                <input type="checkbox" id="readOnly" name="readOnly">
            </div>
        </div>
        <c:if test="${fieldTypeId != 7 && fieldTypeId != 8 && fieldTypeId != 9 && fieldTypeId != 18}">
            <div class="customProperty1 row">
                <div class="col-lg-1 col-md-1">
                    <p>排序:</p>
                </div>
                <div class="col-lg-3 col-md-3">
                    <input type="checkbox" id="canOrder" name="canOrder">
                </div>
            </div>
        </c:if>
    </form>
</div>
<script type="text/javascript">
    //保存字段信息
    function saveFiled(node){
        var flag = validateMethod();
        if(flag){
            $.ajax({
                type: "POST",
                url:"${ctx}/"+Constants.saveFieldNameUrl,
                data:$("#form_field").serialize(),// 要提交的表单
                async:false,
                success:function(data) {
                    alert("添加字段成功");
                    parent.closeTab(1);
                },
                error: function(){
                    alert("添加字段异常");
                }
            });
        }
    }

    //验证表单数据是否合法
    function validateMethod(){
        var flag = true;
        //验证属性英文名只能输入英文
        var fieldValue = $('#fieldName').val();
        if(fieldValue == ""){
            alert("属性名(英文)不能为空!");
            $('#fieldName').focus();
            return false;
        }
        var parent=/^[A-Za-z]+$/;
        if(!parent.test(fieldValue)){
            alert("属性名(英文):只能输入英文!");
            $('#fieldName').focus();
            return false;
        }
        //添加自定义字段时验证字段是否存在
        var url = "${ctx}/"+Constants.validateFieldNameUrl+"?fieldName="+fieldValue+"&entityId=${entityId}&fieldTypeId=${fieldTypeId}";
        $.ajax({
            url    : url,
            async  : false,
            type   : 'POST',
            success : function(data){
                if(data.value == true){
                    flag = false;
                }
            }
        });
        if(!flag){
            alert("字段名称已经存在!");
            $('#fieldName').focus();
            return false;
        }
        //验证属性中文名
        var inchinese = $('#inchinese').val();
        if(inchinese == ""){
            alert("属性名不能为空!");
            $('#inchinese').focus();
            return false;
        }
        //验证取值唯一与批量更新不能同时勾选
        var batchUpdate = $('input[name=batchUpdate]:checked');
        var valueUnique = $('input[name=valueUnique]:checked');
        if(batchUpdate.size() > 0 && valueUnique.size() > 0){
            alert("取值唯一与批量更新不能同时选取!");
            return false;
        }

        $('.num-input').each(function(){
            var v = $(this).val();
            if(v != undefined || v != ''){
                if (isNaN(v)) {
                    alert("请填写数字!");
                    $(this).focus();
                    flag = false;
                    return false;
                }else{
                    if(Number(v) < 0){
                        alert("请输入值!");
                        $(this).focus();
                        flag = false;
                        return false;
                    }
                }
            }
        });
        if( !flag ){
            return false;
        }
        if( 4== ${fieldTypeId}){
            var v = $('input[name=length]').val();
            if(Number(v) > 255 || Number(v) < 1){
                $('[name=length]').focus();
                alert("长度必须是在1到255之间!");
                return false;
            }
        }
        var fieldTypeId = "${fieldTypeId}";
        if("321".indexOf(fieldTypeId) != -1) {
            var v = $('[name=length]').val();
            var p = $('[name=precision]').val();
            if(isNaN(v)){
                $('[name=length]').focus();
                alert("长度必须是数字!");
                return false;
            }
            if(isNaN(p)){
                $('[name=precision]').focus();
                alert("精度必须是数字!");
                return false;
            }
            if((Number(v))>13 || (Number(v))<1 ){
                $('[name=length]').focus();
                alert("字段长度应在1到13之间!");
                return false;
            }
            if((Number(p))>8 || (Number(p))< 0){
                $('[name=precision]').focus();
                alert("精度必须是在0到8之间!");
                return false;
            }
            if(Number(p) >= Number(v)){
                $('[name=precision]').focus();
                alert("字段长度应该大于精度!");
                return false;
            }
            if(Number(v)+Number(p)>15){
                $('[name=length]').focus();
                alert('字段长度应在1到'+(15-Number(p))+'之间');
                return false;
            }

        }
        return true;
    }

</script>
</body>
</html>
